package com.example.robolectricsample

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    lateinit var viewModel: MainViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_main)
        Log.d("GJ", "onCreate ----")

        val button: Button = findViewById(R.id.btn_test)

        button.setOnClickListener {
            onButtonTestClick()
        }

        viewModel = ViewModelProvider(this)[MainViewModel::class.java]

        dataObverse()
        loadData()
    }

    private val condition: Int = 1

    fun loadData() {
        if (condition == 1) {
            viewModel.fetchList()
        } else {

        }
    }

    fun onButtonTestClick() {
        Toast.makeText(this, getString(R.string.toast_text), Toast.LENGTH_SHORT).show()
        val intent = Intent(this, SecondActivity::class.java)
        if (intent.resolveActivity(packageManager) != null) {
            startActivity(intent)
        }
    }

    override fun onResume() {
        super.onResume()
        Log.d("GJ", "onResume ----")
    }

    override fun onPause() {
        super.onPause()
        Log.d("GJ", "onPause ----")
    }

    fun dataObverse() {
        viewModel.mainStringData.observeForever {
            findViewById<TextView>(R.id.txt_value).setText(it)
        }
    }

}